<?php
/**
 * Created by PhpStorm.
 * User: antely
 * Date: 2018/4/26
 * Time: 11:58
 * 用户信息验证类
 */
namespace app\api\validate;
use think\{Validate,Db};
class Anchor extends Validate{
    protected $rule = [
        ['name',            'require|checkExist',                  '-11400'],
        ['sex',                'require',                  '-37003'],
        ['id',                   'require|checkNow',                  '-10022'],
        ['mobile',         'number|checkMobile',                  '-10669'],
        ['anchor_id',                   'require|number|checkAnchorExist',        '-50014|-50013'],

    ];
    protected $scene = [
        'anchor' => ['name','sex','mobile'],
        'delete'=>['id'],
        'follow'=>'anchor_id'
    ];

    public function checkNow($value,$rule,$data){
        $get_one= Db::connect(config('database.db_config1'))->name('Anchor')->where('status','neq',-1)->where(['id'=>$value])->value('id');
        if($get_one){
            return Db::connect(config('database.db_config1'))->name('Live')->where(['status'=>1,'anchor_id'=>$get_one])->where('status','neq',-1)->count()>0?-50037:true;
        }else{
            return -50012;
        }
    }

    public function checkExist($value,$rule,$data){
        if(empty($data['id'])){
            return Db::connect(config('database.db_config1'))->name('Anchor')->where(['name'=>$value])->where('status','neq',-1)->count('id')==1?-10600:true;
        }else{
            return Db::connect(config('database.db_config1'))->name('Anchor')->where(['name'=>$value])->where('id !='.$data['id'].' ')->where('status','neq',-1)->count('id')==1?-10600:true;
        }
    }
    public function checkMobile($value,$rule,$data){
        if(empty($data['id'])){
            return Db::connect(config('database.db_config1'))->name('Anchor')->where(['mobile'=>$value])->where('status','neq',-1)->count('id')==1?-10602:true;
        }else{
            return Db::connect(config('database.db_config1'))->name('Anchor')->where(['mobile'=>$value])->where('id !='.$data['id'].' ')->where('status','neq',-1)->count('id')==1?-10602:true;
        }
    }

    public function checkAnchorExist($value,$rule,$data){
        return Db::connect(config('database.db_config1'))->name('Anchor')->where(['id'=>$value])->where('status','neq',-1)->count('id')==1?true:-50012;
    }

}